package com.fanco.learn_datastruct_leetcode.designpattern.singleton.type8;

/**
 * @author qiaowang 2022/2/9
 * 描述:枚举实现单例模式
 * 优点：不仅能够避免多线程同步问题，还能防止反序列化重新创建新的对象。
 */
public class Singleton08 {
    public static void main(String[] args) {
        Singleton singleton = Singleton.INSTANCE;
        Singleton singleton1 = Singleton.INSTANCE;
        System.out.println(singleton1 == singleton);

        System.out.println(singleton1.hashCode());
        System.out.println(singleton.hashCode());

        singleton.sayOk();
    }
}

enum Singleton {
    INSTANCE; // 属性
    public void sayOk() {
        System.out.println("ok");
    }
}
